package com.wechat.task;

import com.wechat.entity.Question;
import com.wechat.service.QuestionService;
import com.wechat.service.UserBalanceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 问题定时退款任务
 *
 * @author cgc
 * @date 2018/4/23
 */
@Component
public class QuestionRefundTask {

    private final Logger LOG = LoggerFactory.getLogger(QuestionRefundTask.class);

    @Autowired
    private QuestionService questionService;
    @Autowired
    private UserBalanceService userBalanceService;

    @Scheduled(cron = "0 */30 * * * ?")
    public void questionRefund() {

        LOG.info("[QuestionRefundTask]Start");

        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, -2);
        date = calendar.getTime();

        try {
            List<Question> list = questionService.questionJoinRechargeList(null, 1, (int)Question.AMOUNT_STATUS_NOT_TAKE, null, date);

            int right = 0;
            int wrong = 0;

            if(list != null && list.size() > 0){
                for (Question q:list) {
                    if(userBalanceService.questionRefund(q)){
                        right += 1;
                    }else {
                        wrong += 1;
                    }
                }
            }

            LOG.info("[QuestionRefundTask]end:right=" + right + ";wrong=" + wrong);
        }catch (Exception e){
            LOG.error("[QuestionRefundTask]error",e);
        }

    }
}
